package com.kotlin.tsw.kotlinmall.ui.activity

import android.os.Bundle
import android.support.v4.app.Fragment
import com.ashokvarma.bottomnavigation.BottomNavigationBar
import com.kotlin.tsw.baselibrary.ui.activity.BaseActivity
import com.kotlin.tsw.goodscenter.ui.fragment.CategoryFragment
import com.kotlin.tsw.kotlinmall.R
import com.kotlin.tsw.kotlinmall.ui.fragment.HomeFragment
import com.kotlin.tsw.kotlinmall.ui.fragment.MeFragment
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class MainActivity : BaseActivity() {
    private val mHomeFragment by lazy { HomeFragment() }
    private val mCategoryFragment by lazy { CategoryFragment() }
    private val mCartFragment by lazy { HomeFragment() }
    private val mMsgFragment by lazy { HomeFragment() }
    private val mMeFragment by lazy { MeFragment() }
    private val mStack = Stack<Fragment>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
        initFragmnet()
        initBottomBav()
    }

    private fun initView() {
        mBottomNavBar.checkMsgBadge(true)
        mBottomNavBar.checkCartBadge(10)
    }


    private fun initFragmnet() {
        val manager = supportFragmentManager.beginTransaction()
        manager.add(R.id.mContaier, mHomeFragment)
        manager.add(R.id.mContaier, mCategoryFragment)
        manager.add(R.id.mContaier, mCartFragment)
        manager.add(R.id.mContaier, mMsgFragment)
        manager.add(R.id.mContaier, mMeFragment)
        manager.commit()
        mStack.add(mHomeFragment)
        mStack.add( mCategoryFragment)
        mStack.add( mCartFragment)
        mStack.add( mMsgFragment)
        mStack.add( mMeFragment)
    }
    private fun initBottomBav() {
        mBottomNavBar.setTabSelectedListener(object : BottomNavigationBar.OnTabSelectedListener {
            override fun onTabReselected(position: Int) {
            }

            override fun onTabUnselected(position: Int) {
            }

            override fun onTabSelected(position: Int) {
                  changeFragmnet(position)
            }
        })
        changeFragmnet(0)
    }

    private fun changeFragmnet(position: Int) {
        val manager = supportFragmentManager.beginTransaction()
        for (fragment in mStack){
            manager.hide(fragment)
        }
        manager.show(mStack[position])
        manager.commit()
    }
}
